﻿Imports System.Linq
Imports System.Collections.Generic
Imports SISSAT.BusinessLayer.Services
Imports SISSAT.BusinessLayer.Managers
Imports SISSAT.BusinessLayer.DataViews
Imports SISSAT.BusinessLayer.DomainModels

Namespace Views
    Public Class StudentListControlPresenter

        Private Property View As IStudentListControlView
        Private Property EmployeeService As IEmployeeService
        Private Property ScheduleService As IScheduleService
        Private Property ExaminationService As IExaminationService
        Private Property MatriculationService As IMatriculationService

        Public Sub New(view As IStudentListControlView)
            EmployeeService = New EmployeeService
            ScheduleService = New ScheduleService
            ExaminationService = New ExaminationService
            MatriculationService = New MatriculationService

            Me.View = view
            AddHandler Me.View.OnInitialization, AddressOf OnInitialization
        End Sub

        Private Sub OnInitialization(semester As Semesters, schoolYear As Integer)
            If Me.View.ScheduleId < 1 Then
                Return
            End If

            Dim schedule As Schedule = ScheduleService.ReadById(Me.View.ScheduleId)

            Me.View.IsGradeEditable = False
            If DateTime.Now >= schedule.StartDate Then
                Select Case semester
                    Case Semesters.Second
                        If schoolYear = DateTime.Now.Year Then
                            If DateTime.Now.Month >= 12 Then
                                Me.View.IsGradeEditable = True
                            End If
                        ElseIf DateTime.Now <= schedule.EndDate Then
                            Me.View.IsGradeEditable = True
                        End If
                    Case Else
                        If DateTime.Now <= schedule.EndDate Then
                            Me.View.IsGradeEditable = True
                        End If
                End Select
            End If

            Me.View.IsUserAnInstructorForSchedule = (schedule.Employee.EmployeeId = Me.View.InstructorId)

            Dim entities As IEnumerable(Of Employee) = EmployeeService.ReadAllByScheduleId(Me.View.ScheduleId)
            Me.View.RepeaterDataSource = From entity In entities
                                         Let name = String.Format("{0} {1}", entity.FirstName, entity.LastName)
                                         Let majorExams As IEnumerable(Of Examination) = ExaminationService.ReadAllFilteredByMajorExams(entity.EmployeeCode, Me.View.ScheduleId)
                                         Let preliminary = (From prelim In majorExams Where prelim.ExaminationType.ExaminationTypeId = 9 Select prelim.Grade).FirstOrDefault()
                                         Let midterm = (From mt In majorExams Where mt.ExaminationType.ExaminationTypeId = 10 Select mt.Grade).FirstOrDefault()
                                         Let prefinals = (From pf In majorExams Where pf.ExaminationType.ExaminationTypeId = 11 Select pf.Grade).FirstOrDefault()
                                         Let finals = (From f In majorExams Where f.ExaminationType.ExaminationTypeId = 12 Select f.Grade).FirstOrDefault()
                                         Let gradePoint = MatriculationService.ReadGradePoint(entity.EmployeeId, Me.View.ScheduleId)
                                         Select New StudentListDataView With {
                                             .ContactNumber = entity.ContactNumber,
                                             .EmployeeCode = entity.EmployeeCode,
                                             .EmployeeName = name,
                                             .GradePoint = Convert.ToString(gradePoint),
                                             .Finals = Convert.ToString(finals),
                                             .Midterm = Convert.ToString(midterm),
                                             .Prefinals = Convert.ToString(prefinals),
                                             .Preliminary = Convert.ToString(preliminary),
                                             .ScheduleId = Me.View.ScheduleId
                                         }
        End Sub

    End Class
End Namespace

